﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

using Fuse8.ContentManagementFramework.BizFacade.Services;
using Fuse8.ContentManagementFramework.Administration.Models;
using Fuse8.ContentManagementFramework.Domain.Bases;
using Fuse8.DomainFramework.Common.Locator;

namespace Fuse8.ContentManagementFramework.Administration.Controllers
{
    public class CmsAdminHomeController : CmsAdminController
    {
        private CmsDashboardService _cmsDashboardService = new CmsDashboardService();
        private CmsEntityRevisionServiceFacade _cmsEntityRevisionService = new CmsEntityRevisionServiceFacade();
        
        public ActionResult Index()
        {
            return View(_cmsDashboardService.FindByUser(CmsContext.ContextUser));
        }

        public ActionResult QuickTaskPanel()
        {
            var data = new List<CmsQuickTaskPanelModel>();

            data.AddRange(
                new CmsQuickTaskPanelModel[]
                {
                    new CmsQuickTaskPanelModel
                    {
                        TaskActionUrl = "/admin/cmspage/add",   
                        TaskLogo = "/admin/assets/images/360/icons/add-a-page.png",
                        TaskName = "Add a page"
                    },
                    new CmsQuickTaskPanelModel
                    {
                        TaskActionUrl = "/admin/cmsmediacontent/add",   
                        TaskLogo = "/admin/assets/images/360/icons/add-a-media.png",
                        TaskName = "Upload media"
                    },
                    new CmsQuickTaskPanelModel
                    {
                        TaskActionUrl = "/admin-custom/news/addnews",   
                        TaskLogo = "/admin/assets/images/360/icons/add-news.png",
                        TaskName = "Add news item"
                    },
                    new CmsQuickTaskPanelModel
                    {
                        TaskActionUrl = "/admin-custom/products/add",   
                        TaskLogo = "/admin/assets/images/360/icons/add-product.png",
                        TaskName = "Add product"
                    },
                    new CmsQuickTaskPanelModel
                    {
                        TaskActionUrl = "/admin/cmsuser/add",  
                        TaskLogo = "/admin/assets/images/360/icons/add-a-user.png",
                        TaskName = "Add a user"
                    }
                });

            return PartialView(data);
        }

        public ActionResult SiteWorkflowPanel()
        {
            if (SiteService.FindAll().Count() == 1)
            {
                return Json(1);
            }

            var activeItems = _cmsEntityRevisionService.FindAllActiveRevisions(SiteAwareCategory.AllSiteGroups);

            var results = activeItems
                    .Where(e => e.Site != null && e.Site.Id != ContextSite.Id)
                    .GroupBy(e => new {SiteName = e.Site != null? e.Site.SiteName : "All sites", SiteId = e.SiteId})
                    .Select(group => new CmsSiteWorkflowPanelModel
                        {
                            SiteId = group.Key.SiteId,
                            SiteName = group.Key.SiteName,
                            ActiveCount = group.Count(),
                            ExpiredCount = group.Count(o => o.Workflow.ApproveDateDeadline < DateTime.Now),
                            States = new List<CmsSiteWorkflowStateModel>(group.GroupBy(o => o.Workflow.LatestWorkflowState.WorkflowState)
                                .Select(stateGroup => new CmsSiteWorkflowStateModel() {
                                    State = stateGroup.Key,
                                    ActiveCount = stateGroup.Count(),
                                    ExpiredCount = stateGroup.Count(o => o.Workflow.ApproveDateDeadline < DateTime.Now)
                                }).OrderBy(e => e.State))
                        })
                    .OrderBy(e => e.SiteName);

            if (results.Any())
                return PartialView(results);

            return Json(1);
        }
    }
}
